<template>
	<view class="goods-list">
    <view v-for="(item,i) in queryList" :key="i" @click="gotoDetail(item)">
      <my-goods :goods="item"></my-goods>
    </view>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
        queryObj:{
          cid:'',
          queryName:'',
          pageSize:10,
          pageIndex:1
        },
        isLoading:false,//节流阀
        total:0,
        queryList:[],
        defaultImage:'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
			};
		},
    methods:{
     async getQueryList(cb){
       this.isLoading = true;
        const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj);
        this.isLoading = false;
        cb && cb();
        if (res.meta.status !== 200) return uni.$showMsg();
          // 为数据赋值：通过展开运算符的形式，进行新旧数据的拼接
        this.queryList = [...this.queryList,...res.message.goods];
        this.total = res.message.total;
      },
      gotoDetail(item){
        uni.navigateTo({
          url:'../goods_detail/goods_detail?good_id='+ item.good_id
        });
      }
    },
    //上拉加载
    onReachBottom() {
      if(this.queryObj.pageIndex * this.queryObj.pageSize > this.total){
        return uni.$showMsg('数据加载完毕！');
      }
      //正在获取数据
      if(this.isLoading){
        return;
      }
      this.queryObj.pageIndex++;
      this.getQueryList();
    },
    //下拉刷新
    onPullDownRefresh() {
      this.queryObj.pageIndex = 1;
      this.isLoading = false;
      this.queryList = [];
      this.total = 0;
      this.getQueryList(() => uni.stopPullDownRefresh());
    },
    onLoad(option) {
      console.log(option);
      this.queryObj.cid = option.cid || '';
      this.queryObj.queryName = option.query || '';
      this.getQueryList();
    }
	}
</script>

<style lang="scss">

</style>
